﻿using System.Text;
using System.Collections.Generic;
using VIT.Library.Web;
using VIT.DataHelper.DataAccess;


namespace VIT.Web.UI
{
    abstract public class VITTemplate : System.Web.UI.MasterPage, IWebUI
    {
        // URL
        public string URLCurrentPath { get; set; }
        public string URLCurrentPathAndQuery { get; set; }
        public string URLRoot { get; set; }
        public string URLBaseDir { get; set; }

        public VITTemplate()
        {
            URLCurrentPath = URL.getCurrentPath();
            URLCurrentPathAndQuery = URL.getPathAndQuery();
            URLRoot = URL.baseUrl;
            URLBaseDir = URL.baseDir;
        }

        #region inherit interface
        public string linkComponent(string ComponentName, string View = "", string Param = "")
        {
            return URL.linkComponent(ComponentName, View, Param);
        }
        public void RedirectComponent(string ComponentName, string View = "", string Param = "")
        {
            Response.Redirect(URL.linkComponent(ComponentName, View, Param));
        }
        public void TransferComponent(string ComponentName, string View = "", string Param = "")
        {
            Server.Transfer(URL.linkComponent(ComponentName, View, Param));
        }
        public VITContext VITData
        {
            get
            {
                if (Application["VITDataContext"] as VITContext == null)
                {
                    for (int i = 0; i < System.Configuration.ConfigurationManager.ConnectionStrings.Count; i++)
                    {
                        if (SelectConnectionString(System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString))
                        {
                            var strConn = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                            Application["VITDataContext"] = new VITContext(strConn);
                            break;
                        }
                    }
                }
                return Application["VITDataContext"] as VITContext;
            }
        }
        #endregion

        private static bool SelectConnectionString(string ConnectionString)
        {
            System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(ConnectionString);
            try
            {
                sqlConn.Open();
                sqlConn.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // hàm addmodule vào template
        public bool LoadModule(Web.Controls.Skin skin, string moduleName, Dictionary<string, string> pars, string title = "")
        {
            StringBuilder pathModule = new StringBuilder();
            pathModule.Append(URL.appPath);
            if (URL.getCurrentPath().ToLower().Contains("admin")) pathModule.Append("admin/Modules/");
            else pathModule.Append("Modules/");
            pathModule.Append(moduleName);
            pathModule.Append("/");
            pathModule.Append(moduleName);
            pathModule.Append(".ascx");
            var module = LoadControl(pathModule.ToString()) as VITModule;

            StringBuilder pathSkin = new StringBuilder();
            pathSkin.Append(URL.appPath);
            if (URL.getCurrentPath().ToLower().Contains("admin")) pathSkin.Append("admin/Skins/");
            else pathSkin.Append("Skins/");
            pathSkin.Append(skin.SkinName);
            pathSkin.Append(".ascx");
            var controlSkin = LoadControl(pathSkin.ToString()) as VITSkin;

            controlSkin.Title = title;
            module.Params = pars;
            skin.Controls.Add(controlSkin);

            var container = controlSkin.FindControl("PlaceHolderContent");
            container.Controls.Add(module);
            return true;
        }
    }
}
